package com.hqd.ch03.v50.boot.web.context;

import com.hqd.ch03.utils.ObjectUtils;
import com.hqd.ch03.v50.boot.web.server.WebServer;
import com.hqd.ch03.v50.context.ApplicationContext;

public interface WebServerApplicationContext extends ApplicationContext {

    /**
     * Returns {@code true} if the specified context is a
     * {@link WebServerApplicationContext} with a matching server namespace.
     *
     * @param context         the context to check
     * @param serverNamespace the server namespace to match against
     * @return {@code true} if the server namespace of the context matches
     * @since 2.1.8
     */
    static boolean hasServerNamespace(ApplicationContext context, String serverNamespace) {
        return (context instanceof WebServerApplicationContext) && ObjectUtils
                .nullSafeEquals(((WebServerApplicationContext) context).getServerNamespace(), serverNamespace);
    }

    /**
     * Returns the server namespace if the specified context is a
     * {@link WebServerApplicationContext}.
     *
     * @param context the context
     * @return the server namespace or {@code null} if the context is not a
     * {@link WebServerApplicationContext}
     * @since 2.6.0
     */
    static String getServerNamespace(ApplicationContext context) {
        return (context instanceof WebServerApplicationContext)
                ? ((WebServerApplicationContext) context).getServerNamespace() : null;

    }

    /**
     * Returns the {@link WebServer} that was created by the context or {@code null} if
     * the server has not yet been created.
     *
     * @return the web server
     */
    WebServer getWebServer();

    /**
     * Returns the namespace of the web server application context or {@code null} if no
     * namespace has been set. Used for disambiguation when multiple web servers are
     * running in the same application (for example a management context running on a
     * different port).
     *
     * @return the server namespace
     */
    String getServerNamespace();

}
